﻿package;


import com.eclecticdesignstudio.motion.Actuate;
import com.eclecticdesignstudio.motion.easing.Quad;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.Lib;
import nme.Assets;

import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.display.Sprite;
import flash.media.Sound;
import flash.ui.Keyboard;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.filters.GlowFilter;
import haxe.FastList;
import flash.events.MouseEvent;
import flash.Lib;
import haxe.Log;

import flash.utils.Timer;
import haxe.Timer;
import flash.events.Event;
import flash.display.MovieClip;
import flash.media.SoundChannel;

//Pages
// import InstructionPage;
 import CreditsPage;
// import OptionsPage;
 import PauseMenu;
// 
// //Levels
// 
// import CustomSprite;
 import Kongregate;
// import Logo;
// import SoundPlayer;


class Main extends Sprite{
private var ground : Int;
private var currentLevel : Int;
private var damageTaken : Int;
private var difficultly : Int;
private var BaseLevels : Array<BaseLevel>;
// private var CutScences : Array<Intro>;

private var State : String;
private var ContinueGameButton:Sprite;
private var StartGameButton:Sprite;
private var MainMenuButton:Sprite;
private var InstructionsButton:Sprite;
private var CreditsButton:Sprite;
private var OptionsButton:Sprite;
private var MainCharacterPic:CustomSprite;
private var Title:TextField;
 private var myLogo : Logo;

private var label1:TextField;
private var TryAgainPic:Sprite;
private var TryAgainMenu:Sprite;
var myTimer:Timer;
// var music:Sound;          
 private var player:SoundPlayer;

private var BackgroundImage : Sprite;
private var StartGameDifficulty:Sprite;
private var StartGameDifficultyAdded:Bool;
//public var Instruction : InstructionPage;
public var Credit: CreditsPage;
public var OptionsMenu : OptionsPage;
public var MouseImage : Sprite;
public var textBubble : Sprite;
public var textBubbleText : TextField;

var mKeyDown:Array<Bool>;
var pausePosition:Float;
//             music.play();
var volume:Bool;
var sound:Bool;
var framerate:Int;

var kongVar : CKongregate;
public function new(){
super();
initialize();

player = new SoundPlayer(Assets.getSound("assets/Virtutes Instrumenti.mp3"));

volume = true;
sound = true;
myLogo = new Logo();
addChild(myLogo);
  try
  {
    kongVar = new CKongregate();
  }
  catch (msg:Dynamic)
  {
  }
mKeyDown = [];
stage.addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown );
stage.addEventListener(KeyboardEvent.KEY_UP, OnKeyUp );

//preloader.addEventListener(Event.ENTER_FRAME, OnMovieEnter);
}

  private function initialize ():Void {  
  Lib.current.stage.align = StageAlign.TOP_LEFT;
  Lib.current.stage.scaleMode = StageScaleMode.NO_SCALE;
BaseLevels = [];
// CutScences = [];
getLevelData();
//inPostion,inMoveSpeed,inJumpSpeed,inAttackSpeed,inHealth:Int
initLevels();

var txtFormat:TextFormat = new TextFormat();
txtFormat.align = TextFormatAlign.CENTER;

//Add Main to Current Flash
flash.Lib.current.addChild(this);
//Create Menu (own Class?)
Title = new TextField();
    
  Title.x = 50; Title.y = 20;
  Title.width = Constants.flashWidth-100; Title.height = Constants.flashHeight;
  Title.multiline = true;
  var myFormat:TextFormat = new TextFormat();
  myFormat.size = 25;
  myFormat.align = TextFormatAlign.CENTER;
  Title.defaultTextFormat = myFormat;
  
  Title.htmlText = "<font color='#FFFFFF'>Beat em Up</font>";
  Title.selectable = false;


//It seems like there should be a better way to add a bitmap to a Sprite than filling the background with the bitmap. But this works.
StartGameButton = new Sprite();
StartGameButton.graphics.beginFill(0x000000);
StartGameButton.graphics.lineStyle(1,0xFFFFFF);
StartGameButton.graphics.drawRect(0, 0, 250 ,48);
var StartGameButtonText = new TextField();
StartGameButtonText.htmlText = "<font size='26' color='#FFFFFF'>"+"Start Game"+"</font>";
StartGameButtonText.width = 250;
StartGameButtonText.x = 0;
StartGameButtonText.y = 5;
StartGameButtonText.setTextFormat(txtFormat);
StartGameButtonText.selectable = false;
StartGameButtonText.mouseEnabled = false;
StartGameButton.addChild(StartGameButtonText);
StartGameButton.x = 20;
StartGameButton.y = 270;

StartGameButton.buttonMode = true;
StartGameButton.addEventListener(MouseEvent.ROLL_OVER,onStartGameHover);
StartGameButton.addEventListener(MouseEvent.ROLL_OUT,onHoverOut);
StartGameButton.addEventListener(MouseEvent.CLICK, onStartGameButtonClick);

ContinueGameButton = new Sprite();

ContinueGameButton.graphics.beginFill(0x000000);
ContinueGameButton.graphics.lineStyle(1,0xFFFFFF);
ContinueGameButton.graphics.drawRect(0, 0, 250 ,48);
var ContinueGameButtonText = new TextField();
ContinueGameButtonText.htmlText = "<font size='26' color='#FFFFFF'>"+"Continue Game"+"</font>";
ContinueGameButtonText.width = 250;
ContinueGameButtonText.x = 0;
ContinueGameButtonText.setTextFormat(txtFormat);
ContinueGameButtonText.y = 5;
ContinueGameButtonText.selectable = false;
ContinueGameButtonText.mouseEnabled = false;
ContinueGameButton.addChild(ContinueGameButtonText);
ContinueGameButton.x = 20;
ContinueGameButton.y = 330;

ContinueGameButton.buttonMode = true;
ContinueGameButton.addEventListener(MouseEvent.ROLL_OVER,onContinueGameHover);
ContinueGameButton.addEventListener(MouseEvent.ROLL_OUT,onHoverOut);
ContinueGameButton.addEventListener(MouseEvent.CLICK, onContinueGameButtonClick);


InstructionsButton = new Sprite();
InstructionsButton.graphics.beginFill(0x000000);
InstructionsButton.graphics.lineStyle(1,0xFFFFFF);
InstructionsButton.graphics.drawRect(0, 0, 250 ,48);
var InstructionsButtonText = new TextField();
InstructionsButtonText.htmlText = "<font size='26' color='#FFFFFF'>"+"Instructions"+"</font>";
InstructionsButtonText.width = 250;
InstructionsButtonText.x = 0;
InstructionsButtonText.setTextFormat(txtFormat);
InstructionsButtonText.y = 5;
InstructionsButtonText.selectable = false;
InstructionsButtonText.mouseEnabled = false;
InstructionsButton.addChild(InstructionsButtonText);
InstructionsButton.x = 20;
InstructionsButton.y = 0;

InstructionsButton.buttonMode = true;
InstructionsButton.addEventListener(MouseEvent.CLICK, onInstrutionsButtonClick);

// Instruction = new InstructionPage();
// Instruction.addEventListener("Back",onInstrutionsBack);

CreditsButton = new Sprite();
CreditsButton.graphics.beginFill(0x000000);
CreditsButton.graphics.lineStyle(1,0xFFFFFF);
CreditsButton.graphics.drawRect(0, 0, 250 ,48);
var CreditsButtonText = new TextField();
CreditsButtonText.htmlText = "<font size='26' color='#FFFFFF'>"+"Credits"+"</font>";
CreditsButtonText.width = 250;
CreditsButtonText.x = 0;
CreditsButtonText.setTextFormat(txtFormat);
CreditsButtonText.y = 5;
CreditsButtonText.selectable = false;
CreditsButtonText.mouseEnabled = false;
CreditsButton.addChild(CreditsButtonText);
CreditsButton.x = 20;
CreditsButton.y = 390;

CreditsButton.buttonMode = true;
CreditsButton.addEventListener(MouseEvent.ROLL_OVER,onCreditsHover);
CreditsButton.addEventListener(MouseEvent.ROLL_OUT,onHoverOut);
CreditsButton.addEventListener(MouseEvent.CLICK, onCreditsButtonClick);

Credit = new CreditsPage();
Credit.addEventListener("Back",onCreditsBack);

OptionsButton = new Sprite();
OptionsButton.graphics.beginFill(0x000000);
OptionsButton.graphics.lineStyle(1,0xFFFFFF);
OptionsButton.graphics.drawRect(0, 0, 250 ,48);
var OptionsButtonText = new TextField();
OptionsButtonText.htmlText = "<font size='26' color='#FFFFFF'>"+"Options"+"</font>";
OptionsButtonText.width = 250;
OptionsButtonText.x = 0;
OptionsButtonText.setTextFormat(txtFormat);
OptionsButtonText.y = 5;
OptionsButtonText.selectable = false;
OptionsButtonText.mouseEnabled = false;
OptionsButton.addChild(OptionsButtonText);
OptionsButton.x = 20;
OptionsButton.y = 450;

OptionsButton.buttonMode = true;
OptionsButton.addEventListener(MouseEvent.ROLL_OVER,onOptionsHover);
OptionsButton.addEventListener(MouseEvent.ROLL_OUT,onHoverOut);
OptionsButton.addEventListener(MouseEvent.CLICK, onOptionsButtonClick);

OptionsMenu = new OptionsPage();
OptionsMenu.addEventListener("Back",onOptionsMenuBack);
OptionsMenu.addEventListener("ResetSavedData",onOptionsMenuResetSavedData);
OptionsMenu.addEventListener("VolumeToggle",onOptionsMenuVolumeToggle);
OptionsMenu.addEventListener("PrevTrack",onPrevTrack);
OptionsMenu.addEventListener("NextTrack",onNextTrack);
OptionsMenu.addEventListener("SoundEffectsToggle",onOptionsMenuSoundEffectsToggle);
//this.addEventListener(Event.ENTER_FRAME, OnEnter);
  myTimer = new Timer(12);
  myTimer.addEventListener("timer", OnEnter);
  myTimer.start();

  label1 = new TextField();

TryAgainMenu = new Sprite();
//TryAgainMenu.width = Constants.flashWidth;
TryAgainMenu.graphics.beginFill(0x000000);
TryAgainMenu.graphics.drawRect(0,0,Constants.flashWidth,Constants.flashHeight);
TryAgainMenu.graphics.endFill();
label1.width = 600;
label1.height = Constants.flashHeight;
label1.x = 25;
label1.y = 25;
label1.wordWrap = true;
label1.selectable = false;
label1.htmlText = "<font size='30' color='#FFFFFF'>" +"Peace has been restored to Box-ville.  Purged of the pink and interesting characters, the overrated FPS can now rise again.  YAY!"  + "</font>";

TryAgainMenu.addChild(label1);


TryAgainPic = new Sprite();
TryAgainPic.graphics.beginFill(0x000000);
TryAgainPic.graphics.lineStyle(1,0xFFFFFF);
TryAgainPic.graphics.drawRect(0, 0, 46,52);
TryAgainPic.x = 300;
TryAgainPic.y = 75;

MainMenuButton  = new Sprite();
MainMenuButton.graphics.beginFill(0x000000);
MainMenuButton.graphics.lineStyle(1,0xFFFFFF);
MainMenuButton.graphics.drawRect(0, 0, 250 ,48);
var MainMenuButtonText = new TextField();
MainMenuButtonText.htmlText = "<font size='30' color='#FFFFFF'>"+"Main Menu"+"</font>";
MainMenuButtonText.width = 250;
MainMenuButtonText.x = 50;
MainMenuButtonText.y = 5;
MainMenuButtonText.selectable = false;
MainMenuButtonText.mouseEnabled = false;
MainMenuButton.addChild(MainMenuButtonText);
MainMenuButton.x = 200;
MainMenuButton.y = 300;
MainMenuButton.buttonMode = true;
MainMenuButton.addEventListener(MouseEvent.CLICK, onMainMenuButtonClick);



StartGameDifficulty = new Sprite();
StartGameDifficulty.graphics.beginFill(0x555555);
StartGameDifficulty.graphics.drawRect(0,0,330,250);
StartGameDifficulty.graphics.endFill();
StartGameDifficulty.x = 290;
StartGameDifficulty.y = 262;
StartGameDifficultyAdded = false;

var easyGame = new Sprite();
    easyGame.graphics.lineStyle(1,0xFFFFFF);
    easyGame.graphics.beginFill(0x000000);
    easyGame.graphics.drawRect(0, 0, 250 ,48);
    var easyGameText = new TextField();
    easyGameText.htmlText = "<font size='26' color='#FFFFFF'>"+"Easy"+"</font>";
    easyGameText.width = 250;
    easyGameText.x = 0;
    easyGameText.setTextFormat(txtFormat);
    easyGameText.y = 5;
    easyGameText.selectable = false;
    easyGameText.mouseEnabled = false;
    easyGame.addChild(easyGameText);

    StartGameDifficulty.addChild(easyGame);
    easyGame.x = 50;
    easyGame.y = 20;
    easyGame.buttonMode = true;
easyGame.addEventListener(MouseEvent.ROLL_OVER,onEasyGameHover);
easyGame.addEventListener(MouseEvent.ROLL_OUT,onHoverOut);
    easyGame.addEventListener(MouseEvent.CLICK, onEasyGameButtonClick);


var mediumGame = new Sprite();
    mediumGame.graphics.beginFill(0x000000);
    mediumGame.graphics.lineStyle(1,0xFFFFFF);
    mediumGame.graphics.drawRect(0, 0, 250 ,48);
    var mediumGameText = new TextField();
    mediumGameText.htmlText = "<font size='26' color='#FFFFFF'>"+"Medium"+"</font>";
    mediumGameText.width = 250;
    mediumGameText.x = 0;
    mediumGameText.setTextFormat(txtFormat);
    mediumGameText.y = 5;
    mediumGameText.selectable = false;
    mediumGameText.mouseEnabled = false;
    mediumGame.addChild(mediumGameText);

StartGameDifficulty.addChild(mediumGame);
mediumGame.x = 50;
mediumGame.y = 100;

mediumGame.buttonMode = true;
mediumGame.addEventListener(MouseEvent.ROLL_OVER,onMediumGameHover);
mediumGame.addEventListener(MouseEvent.ROLL_OUT,onHoverOut);
mediumGame.addEventListener(MouseEvent.CLICK, onMediumGameButtonClick);


var hardGame = new Sprite();
    hardGame.graphics.lineStyle(1,0xFFFFFF);
    hardGame.graphics.beginFill(0x000000);
    hardGame.graphics.drawRect(0, 0, 250 ,48);
    var hardGameText = new TextField();
    hardGameText.htmlText = "<font size='26' color='#FFFFFF'>"+"Hard"+"</font>";
    hardGameText.width = 250;
    hardGameText.x = 0;
    hardGameText.setTextFormat(txtFormat);
    hardGameText.y = 5;
    hardGameText.selectable = false;
    hardGameText.mouseEnabled = false;
    hardGame.addChild(hardGameText);

    StartGameDifficulty.addChild(hardGame);
    hardGame.x = 50;
    hardGame.y = 175;
    hardGame.buttonMode = true;
    hardGame.addEventListener(MouseEvent.ROLL_OVER,onHardGameHover);
    hardGame.addEventListener(MouseEvent.ROLL_OUT,onHoverOut);
    hardGame.addEventListener(MouseEvent.CLICK, onHardGameButtonClick);


MainCharacterPic = new CustomSprite();
MainCharacterPic.graphics.beginFill(0xffffff,0);
MainCharacterPic.graphics.lineStyle(10,0x000000);
MainCharacterPic.graphics.drawRect(0,0,64,64);
MainCharacterPic.graphics.endFill(); 

var tileSheetColumn:Int = Std.int(Constants.BOX / 10);
var tileSheetRow:Int = Std.int(Constants.BOX % 10);
var sourceRectangle:Rectangle = new Rectangle
  (tileSheetColumn * 64, tileSheetRow * 64, 48,48);
		      
var destinationPoint:Point = new Point(0, 0);
var modelBitmapData = new BitmapData(48, 48, true, 0);
      modelBitmapData.copyPixels
      (
	Assets.getBitmapData("assets/TileSheet.png"), 
	sourceRectangle, 
	destinationPoint,
	null, null, true
      );
var modelBitmap = new Bitmap(modelBitmapData);
//MainCharacterPic.addChild(modelBitmap);
//MainCharacterPic.resize(80,80);
//MainCharacterPic.removeChild(modelBitmap);
MainCharacterPic.x = 310;
MainCharacterPic.y = 190;

//The State Will determine whether to load a level, or show Main Menu, or whatever.  Used in the OnEnterEvent.
State = "InitLoad";
//State = "LoadMainMenu";

//preloader = new PreloaderHost();
BackgroundImage = new Sprite();
BackgroundImage.graphics.beginBitmapFill(Assets.getBitmapData("assets/MainMenu.png"));
//   currentBackground.graphics.drawRect(0, 0, inBackground.width,inBackground.height);
BackgroundImage.graphics.drawRect(0,0,Constants.flashWidth,Constants.flashHeight);
BackgroundImage.graphics.endFill();
	  
  }

public function OnKeyUp (event:KeyboardEvent)
    {
      mKeyDown[event.keyCode] = false;
      //lastUp = event.keyCode;
    }

public function OnKeyDown(event:KeyboardEvent)
   {
      // When a key is held down, multiple KeyDown events are generated.
      // This check means we only pick up the first one.
      if (!mKeyDown[event.keyCode])
      {
         // Store for use in game
         mKeyDown[event.keyCode] = true;
	  //I do this here, because if they hold down P,
	 // it won't pause and unpause constantly.
	  if(mKeyDown[ 77 ] == true)
	  {
	    volume = !volume;
	    for(level in 0...BaseLevels.length)
	    {
	      BaseLevels[level].volume = volume;
	      BaseLevels[level].pauseMenu.renderVolume(volume);
	    }
	  }
      }
  }

function loadMenu()
{
  addChild(BackgroundImage);
  addChild(Title);
  addChild(StartGameButton);
  addChild(ContinueGameButton);
  //addChild(InstructionsButton);
  addChild(CreditsButton);
  addChild(OptionsButton);
  addChild(MainCharacterPic);
  //addChild(textBubble);
  //addChild(MouseImage);
}
function removeMenu()
{
  removeChild(Title);
  removeChild(StartGameButton);
  removeChild(ContinueGameButton);
  removeChild(BackgroundImage);
  //removeChild(InstructionsButton);
  removeChild(CreditsButton);
  removeChild(OptionsButton);
  removeChild(MainCharacterPic);
  //removeChild(textBubble);
  //removeChild(MouseImage);
  //removeChild(KeysImage);
  if(StartGameDifficultyAdded)
    {removeChild(StartGameDifficulty);StartGameDifficultyAdded=false;}
}

//Change to stat Menu
function loadTryAgainMenu(text:String)
{
  try
  {
    
  }
  catch(msg: Dynamic) 
  {
    
  }
  addChild(TryAgainMenu);
  addChild(MainMenuButton);
  //addChild(TryAgainPic);
  //player.stop();
}
function removeTryAgainMenu()
{
 removeChild(TryAgainMenu);
 removeChild(MainMenuButton);
 //removeChild(TryAgainPic);
}
function initLevels()
{



  
  //CutScences[0] = new PinkIntro(flash.Lib.current.stage);
  //
  BaseLevels[0] = new Level1(Lib.current.stage);

//   CutScences[1] = new BlueIntro(flash.Lib.current.stage);
  BaseLevels[1] = new CircleCollisionLevel(Lib.current.stage);
  BaseLevels[2] = new SquareCollisionLevel(Lib.current.stage);
   BaseLevels[3] = new CircleSquareLevel(Lib.current.stage);
//   CutScences[2] = new FairyIntro(flash.Lib.current.stage);
   BaseLevels[4] = new CircleLineLevel(Lib.current.stage);
   BaseLevels[5] = new CollisionArrayLevel(Lib.current.stage);
// 
//   CutScences[3] = new PinkFairyIntro(flash.Lib.current.stage);
//   BaseLevels[3] = new Level4(flash.Lib.current.stage);
// 
//   CutScences[4] = new FlowerIntro(flash.Lib.current.stage);
//   BaseLevels[4] = new Level5(flash.Lib.current.stage);
// 
//   CutScences[5] = new HeartIntro(flash.Lib.current.stage);
//   BaseLevels[5] = new Level6(flash.Lib.current.stage);
// 
//   CutScences[6] = new KittyKatIntro(flash.Lib.current.stage);
//   BaseLevels[6] = new Level7(flash.Lib.current.stage);
// 
//   CutScences[7] = new BossIntro(flash.Lib.current.stage);
//   BaseLevels[7] = new BossLevel(flash.Lib.current.stage);

}

function OnEnter(e:flash.events.Event)
{
if(player !=null)
  {
      if(!volume)
      {
	 pausePosition = player.getPosition();
         player.stop();         
      }
      else
      {
        player.play(pausePosition);
      }
  }
if(State == "LoadMainMenu")
{
 loadMenu();
 State = "MainMenu";
}
if(State == "MainMenu" )
   {
     return;
   }
if(State == "TryAgain")
  {
    AnimateTryAgainPic();
    return;
  }
if(State == "InitLoad")
  {
   
//     addChild(preloader);
//     preloader.play();
    State = "InitLoading";
  }
if(State == "InitLoading")
   { 
    //Always Playing. Possible error with swfmill
    //Skipping for now.
 
//     if(!preloader.playing)
//        {
//         preloader.stop();
//         removeChild(preloader);
        State = "LoadingLogo";
//        }

   }
if(State == "LoadingLogo")
  {

     if(!myLogo.play())
     {
       State = "StartGame";
     }
  }
if(State == "StartGame")
   {
    try
    {
    kongVar.SubmitStat("StartGame",1);
    }
    catch(e:Dynamic)
    {
    }
    //player.play(pausePosition);
    currentLevel =1;
    loadCutScence();
   }
if(State == "ContinueGame")
   {
    //player.play(pausePosition);
    loadCutScence();
   }
if(State == "LoadLevel")
{
loadCurrentLevel();
}
if(State == "Playing")
   {
      if(!volume)
      {
	 //pausePosition = player.getPosition();
         //player.stop();
         
      }
      else
      {
        //player.play(pausePosition);
      }
      return;
   }
// try
// {
// //             var req:flash.net.URLRequest = new flash.net.URLRequest("http://av.adobe.com/podcast/csbu_dev_podcast_epi_2.mp3");
// //             
// //             Sound.load(req);
// var music = new BackgroundMusic();            
//             music.play();
// }
// catch (err:flash.Error)
// {
// trace(err.message);
// }


}
public function AnimateTryAgainPic()
{
var randomNumber = Std.random(24);
if(randomNumber == 1)
  TryAgainPic.x = TryAgainPic.x + 5;
if(randomNumber == 2)
  TryAgainPic.x = TryAgainPic.x - 5;

}

public function loadCutScence() : Bool
{

//   if(currentLevel > CutScences.length && CutScences[currentLevel-1]==null)
//     {State="Playing";loadCurrentLevel();}
//   CutScences[currentLevel-1].Load();
//   CutScences[currentLevel-1].addEventListener("LevelFinished",onLevelFinished);
//   State="CutScence";
  return loadCurrentLevel();
}
//Loads Level based on Current Level
public function loadCurrentLevel() : Bool
{
  State = "Playing";
  if(currentLevel > BaseLevels.length)
    {State="TryAgain";loadTryAgainMenu("YOU WON");}
  BaseLevels[currentLevel-1].addEventListener("LevelFinished",onLevelFinished);
  BaseLevels[currentLevel-1].addEventListener("RequestLevel",onLevelRequest);
  BaseLevels[currentLevel-1].addEventListener("LevelQuit",onLevelQuit);
  BaseLevels[currentLevel-1].addEventListener("VolumeToggle",onBaseLevelVolumeToggle);
  BaseLevels[currentLevel-1].addEventListener("PrevTrack",onPrevTrack);
  BaseLevels[currentLevel-1].addEventListener("NextTrack",onNextTrack);

  State = "Playing";
//Level1.Load();
  BaseLevels[currentLevel-1].Load(volume,sound,kongVar,1);
// BaseLevels[currentLevel-1].pauseMenu.renderVolume(volume);
// BaseLevels[currentLevel-1].pauseMenu.renderSoundEffects(sound);
// BaseLevels[currentLevel-1].addEventListener("SoundEffectsToggle",onBaseLevelSoundEffectsToggle);
   return true;
}


public function getLevelData()
{

}
public function removeClip(preloader)
{
 removeChild(preloader);
}
private function onStartGameButtonClick(event:MouseEvent) {
addChild(StartGameDifficulty);
StartGameDifficultyAdded = true;
//removeMenu();
//State = "StartGame";
}
private function onEasyGameButtonClick(event:MouseEvent)
{
removeMenu();
State = "StartGame";
}

private function onMediumGameButtonClick(event:MouseEvent)
{
removeMenu();
State = "StartGame";
}

private function onHardGameButtonClick(event:MouseEvent)
{
removeMenu();
State = "StartGame";
}

private function onContinueGameButtonClick(event:MouseEvent) {
removeMenu();
State = "ContinueGame";
}

//Hover Texts
 private function onStartGameHover(event:MouseEvent)
  {
//     textBubbleText.htmlText = "<font size='16' color='#000000'>This will erase saved game information</font>"; 
  }

private function onContinueGameHover(event:MouseEvent)
{
//     textBubbleText.htmlText = "<font size='16' color='#000000'>Continue an Existing Game</font>";
}

private function onOptionsHover(event:MouseEvent)
{
//     textBubbleText.htmlText = "<font size='16' color='#000000'>Disable Sound or Reset Saved Data</font>";
//     textBubbleText.autoSize = TextFieldAutoSize.CENTER;
}
private function onCreditsHover(event:MouseEvent)
{
//     textBubbleText.htmlText = "<font size='16' color='#000000'>This is where I give people credit for making the game. Mostly me.</font>";
//     textBubbleText.autoSize = TextFieldAutoSize.CENTER;
}


private function onEasyGameHover(event:MouseEvent)
{
//     textBubbleText.htmlText = "<font size='16' color='#000000'>You have 20 health and enemies have 5 health. It will be a piece of cake</font>";
//     textBubbleText.autoSize = TextFieldAutoSize.CENTER;
}


private function onMediumGameHover(event:MouseEvent)
{
//     textBubbleText.htmlText = "<font size='16' color='#000000'>You have 10 health and enemies have 10 health. This may take some effort</font>";
//     textBubbleText.autoSize = TextFieldAutoSize.CENTER;
}

private function onHardGameHover(event:MouseEvent)
{
//     textBubbleText.htmlText = "<font size='16' color='#000000'>You have 8 health and enemies have 15 health.  Only for the Hard Core gamers.</font>";
//     textBubbleText.autoSize = TextFieldAutoSize.CENTER;
}

private function onHoverOut(event:MouseEvent)
{	
  //textBubbleText.htmlText = "<font size='16' color='#000000'>I'm Boxy!</font>";
}


private function onLevelFinished(event:Event)
{

    removeLevelEventListeners();

    currentLevel = 1;
    loadCurrentLevel();
}

private function onLevelRequest(event:RequestLevelEvent)
{

    removeLevelEventListeners();

    if(event.Level == "Circle")
    {
      currentLevel = 2;
    }
    if(event.Level == "Square")
    {
      currentLevel = 3;
    }
    if(event.Level == "CircleSquare")
    {
      currentLevel = 4;
    }
    if(event.Level == "CircleLine")
    {
      currentLevel = 5;
    }
    if(event.Level == "CollisionArray")
    {
      currentLevel = 6;
    }
    loadCurrentLevel();
}

private function onLevelQuit(event:Event)
{
  removeLevelEventListeners();
  State = "InitLoading";
}

private function removeLevelEventListeners()
{
  BaseLevels[currentLevel-1].removeEventListener("LevelFinished",onLevelFinished);
  BaseLevels[currentLevel-1].removeEventListener("RequestLevel",onLevelRequest);
  BaseLevels[currentLevel-1].removeEventListener("LevelQuit",onLevelQuit);
  BaseLevels[currentLevel-1].removeEventListener("PrevTrack",onPrevTrack);
  BaseLevels[currentLevel-1].removeEventListener("NextTrack",onNextTrack);
}

private function removeCutScencesEventListeners()
{
  //CutScences[currentLevel-1].removeEventListener("LevelFinished",onLevelFinished);
}
private function onMainMenuButtonClick(event:MouseEvent) {
removeTryAgainMenu();
State = "LoadMainMenu";
}
private function onInstrutionsButtonClick(event:MouseEvent) {
//removeMenu();
//addChild(Instruction);
}

private function onCreditsButtonClick(event:MouseEvent) {
removeMenu();
addChild(Credit);
}

private function onOptionsButtonClick(event:MouseEvent) {
removeMenu();
addChild(OptionsMenu);
OptionsMenu.renderVolume(volume);

OptionsMenu.renderSoundEffects(sound);
}

// private function onInstrutionsBack(event:InstructionPageEvent)
// {
// removeChild(Instruction);
// loadMenu();
// }

private function onOptionsMenuBack(event:OptionsPageEvent)
{
removeChild(OptionsMenu);
loadMenu();
}
private function onPrevTrack(event:Event)
{
//   if(volume && player != null)
//     {player.getPrevTrack();}
}
private function onNextTrack(event:Event)
{
//   if(volume && player != null)
//     {player.getNextTrack();}
}

private function onOptionsMenuVolumeToggle(event:OptionsPageEvent)
{
volume = !volume;
OptionsMenu.renderVolume(volume);
}
private function onOptionsMenuSoundEffectsToggle(event:OptionsPageEvent)
{
sound = !sound;
OptionsMenu.renderSoundEffects(sound);
}

private function onBaseLevelVolumeToggle(event:PauseMenuEvent)
{
volume = !volume;
  for(level in 0...BaseLevels.length)
  {
    BaseLevels[level].volume = volume;
    BaseLevels[level].pauseMenu.renderVolume(volume);
  }
}
private function onBaseLevelSoundEffectsToggle(event:PauseMenuEvent)
{
sound = !sound;
BaseLevels[currentLevel-1].sound = sound;
//BaseLevels[currentLevel-1].pauseMenu.renderSoundEffects(sound);
}
private function onOptionsMenuResetSavedData(event:OptionsPageEvent)
{
      getLevelData();
      pausePosition = 0;
      currentLevel = 1;
}
private function onCreditsBack(event:CreditsPageEvent)
{
removeChild(Credit);
loadMenu();
}
	
	// Event Handlers
	
	
	
	
	// Entry point
	
	
	
	
	public static function main () {
		
		Lib.current.addChild (new Main ());
		
	}
	
	
}
